package com.my.common;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

@Component
public class ExceptionHandle implements HandlerExceptionResolver {

  @Override
  public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
    Object handler, Exception ex) {
    PrintWriter out = null;
    response.setContentType("application/json;charset=utf-8");
    response.setCharacterEncoding("utf-8");
    try {
      out = response.getWriter();
      out.println(JsonUtil.error("服务端异常！"));
      out.flush();
    } catch (IOException e) {
      // e.printStackTrace();
    } finally {
      if (out != null)
        out.close();
    }
    return null;
  }
}
